﻿<span style='font-size:11px; color:#000000; background:#ffffff; '><pre>
<span style='color:#800000; font-weight:bold; '>protected</span> <span style='color:#800000; font-weight:bold; '>void</span> convertToPdfButton_Click<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>object</span> sender<span style='color:#808030; '>,</span> EventArgs e<span style='color:#808030; '>)</span>
<span style='color:#800080; '>{</span>
    <span style='color:#696969; '>// Create a HTML to Image converter object with default settings</span>
    HtmlToImageConverter htmlToImageConverter <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> HtmlToImageConverter<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set license key received after purchase to use the converter in licensed mode</span>
    <span style='color:#696969; '>// Leave it not set to use the converter in demo mode</span>
    htmlToImageConverter<span style='color:#808030; '>.</span>LicenseKey <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set HTML Viewer width in pixels which is the equivalent in converter of the browser window width</span>
    htmlToImageConverter<span style='color:#808030; '>.</span>HtmlViewerWidth <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>int</span><span style='color:#808030; '>.</span>Parse<span style='color:#808030; '>(</span>htmlViewerWidthTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set HTML viewer height in pixels to convert the top part of a HTML page </span>
    <span style='color:#696969; '>// Leave it not set to convert the entire HTML</span>
    <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>htmlViewerHeightTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>.</span>Length <span style='color:#808030; '>></span> <span style='color:#008c00; '>0</span><span style='color:#808030; '>)</span>
        htmlToImageConverter<span style='color:#808030; '>.</span>HtmlViewerHeight <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>int</span><span style='color:#808030; '>.</span>Parse<span style='color:#808030; '>(</span>htmlViewerHeightTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set if the created image has a transparent background</span>
    htmlToImageConverter<span style='color:#808030; '>.</span>TransparentBackground <span style='color:#808030; '>=</span> SelectedImageFormat<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>=</span><span style='color:#808030; '>=</span> System<span style='color:#808030; '>.</span>Drawing<span style='color:#808030; '>.</span>Imaging<span style='color:#808030; '>.</span>ImageFormat<span style='color:#808030; '>.</span>Png ? transparentBackgroundCheckBox<span style='color:#808030; '>.</span>Checked <span style='color:#808030; '>:</span> <span style='color:#800000; font-weight:bold; '>false</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set the maximum time in seconds to wait for HTML page to be loaded </span>
    <span style='color:#696969; '>// Leave it not set for a default 60 seconds maximum wait time</span>
    htmlToImageConverter<span style='color:#808030; '>.</span>NavigationTimeout <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>int</span><span style='color:#808030; '>.</span>Parse<span style='color:#808030; '>(</span>navigationTimeoutTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed</span>
    <span style='color:#696969; '>// Set this property to 0 if you don't need to wait for such asynchcronous operations to finish</span>
    <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>conversionDelayTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>.</span>Length <span style='color:#808030; '>></span> <span style='color:#008c00; '>0</span><span style='color:#808030; '>)</span>
        htmlToImageConverter<span style='color:#808030; '>.</span>ConversionDelay <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>int</span><span style='color:#808030; '>.</span>Parse<span style='color:#808030; '>(</span>conversionDelayTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    System<span style='color:#808030; '>.</span>Drawing<span style='color:#808030; '>.</span>Image<span style='color:#808030; '>[</span><span style='color:#808030; '>]</span> imageTiles <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span><span style='color:#800080; '>;</span>

    <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>convertUrlRadioButton<span style='color:#808030; '>.</span>Checked<span style='color:#808030; '>)</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#800000; font-weight:bold; '>string</span> url <span style='color:#808030; '>=</span> urlTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Convert the HTML page given by an URL to a set of Image objects</span>
        imageTiles <span style='color:#808030; '>=</span> htmlToImageConverter<span style='color:#808030; '>.</span>ConvertUrlToImageTiles<span style='color:#808030; '>(</span>url<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
    <span style='color:#800000; font-weight:bold; '>else</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#800000; font-weight:bold; '>string</span> htmlString <span style='color:#808030; '>=</span> htmlStringTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>string</span> baseUrl <span style='color:#808030; '>=</span> baseUrlTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Convert a HTML string with a base URL to a set of Image objects</span>
        imageTiles <span style='color:#808030; '>=</span> htmlToImageConverter<span style='color:#808030; '>.</span>ConvertHtmlToImageTiles<span style='color:#808030; '>(</span>htmlString<span style='color:#808030; '>,</span> baseUrl<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>

    <span style='color:#696969; '>// Save the first image tile to a memory buffer</span>

    System<span style='color:#808030; '>.</span>Drawing<span style='color:#808030; '>.</span>Image outImage <span style='color:#808030; '>=</span> imageTiles<span style='color:#808030; '>[</span><span style='color:#008c00; '>0</span><span style='color:#808030; '>]</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Create a memory stream where to save the image</span>
    System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>MemoryStream imageOutputStream <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>MemoryStream<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Save the image to memory stream</span>
    outImage<span style='color:#808030; '>.</span>Save<span style='color:#808030; '>(</span>imageOutputStream<span style='color:#808030; '>,</span> SelectedImageFormat<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Write the memory stream to a memory buffer</span>
    imageOutputStream<span style='color:#808030; '>.</span>Position <span style='color:#808030; '>=</span> <span style='color:#008c00; '>0</span><span style='color:#800080; '>;</span>
    <span style='color:#800000; font-weight:bold; '>byte</span><span style='color:#808030; '>[</span><span style='color:#808030; '>]</span> outImageBuffer <span style='color:#808030; '>=</span> imageOutputStream<span style='color:#808030; '>.</span>ToArray<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Close the output memory stream</span>
    imageOutputStream<span style='color:#808030; '>.</span>Close<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Send the image as response to browser</span>

    <span style='color:#800000; font-weight:bold; '>string</span> imageFormatName <span style='color:#808030; '>=</span> imageFormatComboBox<span style='color:#808030; '>.</span>SelectedValue<span style='color:#808030; '>.</span>ToLower<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set response content type</span>
    Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Type</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>image/</span><span style='color:#800000; '>"</span> <span style='color:#808030; '>+</span> <span style='color:#808030; '>(</span>imageFormatName <span style='color:#808030; '>=</span><span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>jpg</span><span style='color:#800000; '>"</span> ? <span style='color:#800000; '>"</span><span style='color:#0000e6; '>jpeg</span><span style='color:#800000; '>"</span> <span style='color:#808030; '>:</span> imageFormatName<span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Instruct the browser to open the image file as an attachment or inline</span>
    Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Disposition</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> String<span style='color:#808030; '>.</span>Format<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>attachment; filename={0}; size={1}</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>HTML_to_Image.</span><span style='color:#800000; '>"</span> <span style='color:#808030; '>+</span> imageFormatName<span style='color:#808030; '>,</span> outImageBuffer<span style='color:#808030; '>.</span>Length<span style='color:#808030; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Write the PDF document buffer to HTTP response</span>
    Response<span style='color:#808030; '>.</span>BinaryWrite<span style='color:#808030; '>(</span>outImageBuffer<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// End the HTTP response and stop the current page processing</span>
    Response<span style='color:#808030; '>.</span>End<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
<span style='color:#800080; '>}</span>
</pre>
</span>